package structure;

import java.util.Arrays;

public class MyStack1<E> {
    private static final int DEFAULT_CAPACITY = 10;
    private Object[] elem;
    private int size;

    public MyStack1() {
        this.elem = new Object[DEFAULT_CAPACITY];
    }

    private boolean isFull() {
        return size == elem.length;
    }

    public void push(int val) {
        if (isFull()) {
            this.elem = Arrays.copyOf(elem, elem.length * 2);
        }
        elem[size++] = val;
    }

    private boolean isEmpty() {
        return size == 0;
    }

    public E pop() {
        if (isEmpty()) {
            return null;
        }

        return (E) elem[--size];
    }

    public E peek() {
        if (isEmpty()) {
            return null;
        }
        return (E) elem[size-1];
    }

    public int size() {
        return size;
    }
}
